const express = require('express');
const path = require("path");
const history = require('connect-history-api-fallback');
const bodyParser = require('body-parser');
const app = express();
const sysGroupService = require('./service/sysgroup');
const sysService = require("./service/sys");
const serverService = require("./service/server");
const execCommand = require("./shell/shell");
const logger = require('./log').logger;
const connectLogger = require('./log').connectLogger;

app.use(history({
    // verbose: true,
}));

app.use(express.static(__dirname + '/dist'));

app.use(connectLogger());

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
 
// parse application/json
app.use(bodyParser.json());

app.route('/queryGroup.do')
    .all((req, res, next) => {
        sysGroupService.queryAllGroup(req, res);
    });

app.route('/querySys.do')
    .all((req, res, next) => {
        sysService.queryAllSys(req, res);
    });

app.route('/addSys.do')
    .all((req, res, next) => {
        sysService.addSys(req, res);
    });

app.route('/updateSys.do')
    .all((req, res, next) => {
        sysService.updateSys(req, res);
    });

app.route('/deleteSys.do')
    .all((req, res, next) => {
        sysService.deleteSys(req, res);
    });

app.route('/queryServer.do')
    .all((req, res, next) => {
        serverService.queryServerBySysId(req, res);
    });

app.route('/addServer.do')
    .all((req, res, next) => {
        serverService.addServer(req, res);
    });

app.route('/updateServer.do')
    .all((req, res, next) => {
        serverService.updateServer(req, res);
    });

app.route('/deleteServer.do')
    .all((req, res, next) => {
        serverService.deleteServer(req, res);
    });

app.route('/execCommand.do')
    .all((req, res, next) => {
        execCommand(req, res);
    });

const server = app.listen(3000, function() {
    logger.info('Listening on port %d', server.address().port);
});